<?php
/*
Template Name: Post Bab Baru
*/
?>
<?php
$book_id= isset( $_GET['bookid'] ) ? intval( $_GET['bookid'] ) : 0;
$bab_id = isset( $_GET['pid'] ) ? intval( $_GET['pid'] ) : 0;
$edit = isset( $_GET['edit'] );
$addnew = isset( $_GET['addnew'] );
$number ;
$postbuku = get_post($book_id);
$statusbuku = $postbuku ->post_status;
if ($edit=='yes' || $addnew=='yes'){
	if (!current_user_can( 'manage_options' )) //check if user login as admin
	{
		$authorid =get_post_field( 'post_author', $book_id );
		if ( current_user_can('edit_posts', $bab_id)  || current_user_can('edit_posts', $book_id))
		{
			 if ($authorid != get_current_user_id())  
			 {
			 	wp_redirect(get_site_url());
			}
		//	echo "this user is editor";
		}
	}
	//else echo "this user is admin!";
}

if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] )) {

	// Do some minor form validation to make sure there is content
	if (isset($_POST['publish'])) {
    		$error = "";

    	if (!empty($_POST['title'])) {
    		$title = $_POST['title'];
   	 } else {
   	 	$error .= "Judul cerita ";
   	}

    	if (!empty($_POST['description'])&& trim($_POST['description'])!=='') {
    		$description = $_POST['description'];
   	 } else {
   		$error .= "Isi Bab ";
   	}
	
		$tags = $_POST['post_tags'];
		$book_information = array(
				'ID' => $book_id,
				);

		// ADD THE FORM INPUT TO $new_post ARRAY
		if (empty($error)) {
			$new_post = array(
			'post_title'	=>	$title,
			'post_content'	=>	$description,
			'post_status'	=>	'publish',           // Choose: publish, preview, future, draft, etc.
			'post_type'	=>	'chapter',  //'post',page' or use a custom post type if you want to
			//'winerating'	=>	$winerating
		);
			$post_information = array(
				'ID' => $bab_id,
				'post_title' =>  $title,
				'post_content' => $description,
				'post_type' => 'chapter',
				'post_status' => 'publish'
);
		
		//SAVE THE POST
		if ($edit=='yes'){
		$pid=wp_update_post($post_information);
		wp_update_post($book_information);
		}
		else {
			if ($statusbuku=='publish')
			{
				wp_update_post($book_information);
				$pid = wp_insert_post($new_post); }
			else {
				wp_insert_post($new_post);
		}
}
        //KEEPS OUR COMMA SEPARATED TAGS AS INDIVIDUAL
	//	wp_set_post_tags($pid, $_POST['post_tags']);

		//REDIRECT TO THE NEW POST ON SAVE
	//$link = get_permalink( $book_id)."?pid=".$pid."&".$title;
	//wp_redirect( $link );
$link =  get_site_url() ."/daftarcerita/";
		wp_redirect( $link );
		//ADD OUR CUSTOM FIELDS 
			if ($edit!='yes'){
		add_post_meta($book_id, 'Chapter', $pid, false);
		add_post_meta($pid, 'number', $k, true);		}

			
		} // END SAVING POST
	} // END VALIDATION  END PUBLISH
		else// (isset($_POST['draft'])) 
{
    		$error = "";

    	if (!empty($_POST['title'])) {
    		$title = $_POST['title'];
   	 } else {
   	 	$error .= "Judul Cerita ";
   	}
		if (!empty($_POST['description'])&& trim($_POST['description'])!=='') {
    		$description = $_POST['description'];
   	 } 

		$tags = $_POST['post_tags'];
		//$winerating = $_POST['winerating'];

		// ADD THE FORM INPUT TO $new_post ARRAY
		if (empty($error)) {
			$new_post = array(
			'post_title'	=>	$title,
			'post_content'	=>	$description,
			'post_status'	=>	'draft',           // Choose: publish, preview, future, draft, etc.
			'post_type'	=>	'chapter',  //'post',page' or use a custom post type if you want to
		
		);
		$post_information = array(
				'ID' => $bab_id,
				'post_title' =>  $title,
				'post_content' => $description,
				'post_type' => 'chapter',
				'post_status' => 'draft'
);

		//SAVE THE POST
		if ($edit=='yes'){
		$pid=wp_update_post($post_information);
			wp_update_post($book_information);
		}
		else {
		$pid = wp_insert_post($new_post); }

        //KEEPS OUR COMMA SEPARATED TAGS AS INDIVIDUAL
		wp_set_post_tags($pid, $_POST['post_tags']);

		//REDIRECT TO THE NEW POST ON SAVE
		//$link = get_permalink( $pid );
		$link =  get_site_url() ."/daftarcerita/";
		wp_redirect( $link );

		//ADD OUR CUSTOM FIELDS 
		if ($edit!='yes'){
		add_post_meta($book_id, 'Chapter', $pid, false); 
		add_post_meta($pid, 'number', $k, true);	}
	

			//INSERT OUR MEDIA ATTACHMENTS
			if ($_FILES) {
				foreach ($_FILES as $file => $array) {
				$newupload = insert_attachment($file,$pid);
				// $newupload returns the attachment id of the file that
					// was just uploaded. Do whatever you want with that now.
				}

			} // END THE IF STATEMENT FOR FILES */
		} // END SAVING POST
	} // END VALIDATION
} // END THE IF STATEMENT THAT STARTED THE WHOLE FORM

//POST THE POST YO
do_action('wp_insert_post', 'wp_insert_post');

?>

<?php get_header(); ?>
<script type="text/javascript">
 $(function(){
 
})();
</script>
    <div class="col-md-12 column content">
	<div class="row clearfix">
		
		<?php get_template_part( 'kolompertama2' ); ?>
	
       
		<div class="col-md-9 column" > <!-- untuk kolom kedua yang dibagi 2 -->
		<div class="row clearfix">
		
				<div class="col-md-12 column">
				 <?php if(is_user_logged_in()){?>
                	<h8 class="judulprofil">Tulis Bab   </h8>
					<?php //if ( have_posts() ) while ( have_posts() ) : the_post(); 
					if (!empty($bab_id)){
					$postbab = get_post($bab_id);
					$title = $postbab->post_title;
					$content=$postbab->post_content;
					}
					?>
                    <br/>
                    <br/>
					<?php 
					        if (!empty($error)) {
	                            echo  "<strong>Tidak dapat memproses permintaan Anda, mohon isi dahulu : " . $error . ""  ;
	                        } elseif (!empty($success)) {
	                            echo $success ;
	                        }
	                    ?>
					<form id="new_post" name="new_post" method="post" action="" enctype="multipart/form-data">
                    <div>
                  
                    <div class="list-group-item" style="border-left:none; border-right:none;">
                        <div class="row">
                        <div class="col-md-2">Judul Bab</div>
                            <div class="col-md-9 ">
                                <div class="col-sm-12">
                                    <input class="form-control tinggitextbox" id="titlebab" onkeyup="titlevalidation();" type="text" name="title" placeholder="Judul Cerita Wajib diisi" 
									value="<?php //if (!empty($_POST['title'])) 
									echo $title ?>">
									
									
                                </div>
                            </div>
							<label id="errorjudulbab" style="color:red; font-size: 10px;"></label>
                        </div>
                    	</div>
               
                    <br/>
					
						 <div class="col-md-2">Isi Bab</div><br/>
                    <div class="panel panel-default">
						   
                        <textarea class="kotaktext" name="description" id="descriptionbab" ><?php 
						if ($edit=='yes'){echo $content;}
						else 
						{if (!empty($_POST['description'])) 
							{ echo htmlspecialchars($_POST['description']);}
						} ?>
                        </textarea>
                        </div>
						<label id="errorisibab" style="color:red; font-size: 10px;"></label>
               
                   
                    <br/> 
                    <br/>
					<?php if ($addnew=="yes"){?>
					<a  class="btn btn-sm" id="DoDraftBab" > Simpan Konsep</a>
					<input type="submit" value="Simpan Konsep" tabindex="40" id="draftbab" name="draft" class="btn btn-sm" style="display:none;" />
	   				<input type="hidden" name="action" value="draft_post" /></input>
					<?php } if (!($statusbuku=="draft")){ ?>
					<a id="DoPublishBab" class="btn btn-sm"> Simpan dan Tayangkan</a>
					<input type="submit" value="Simpan dan Tayangkan" tabindex="40" id="publishbab" name="publish" class="btn btn-sm" style="display:none;" />
	   				<input type="hidden" name="action" value="publish_post" /></input>
					<?php }wp_nonce_field( 'new-post' ); ?>
                   </div>
                    <!--<button type="submit" class="btn btn-sm">Batal</button>-->
					</form><?php //endwhile;
					} else 
					{?>
					<br/><br/><br/><br/><br/><br/>
					<h9 >Silakan masuk dulu.</h9>
					<?php }					?>
                    </div>
           			
       </div>      
	   </div>   	
	   
       </div><!-- end row clearfix -->
</div> <!--end col-md-12 column content-->
	<!-- end untuk content -->
   
    
    <!-- untuk footer -->

<?php get_footer(); ?>